package com.example.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity {

    //服务器上的图片地址
    private static final String picURL="http://www.neuedu.cn/imgcode";

    private Button btnFirst;
    private ImageView image;
    private Bitmap bitmap=null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnFirst = (Button)findViewById(R.id.btnFirst);
        image = (ImageView) findViewById(R.id.image);
        btnFirst.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                // 启动一个后台线程
                new Thread(){
                    @Override
                    public void run() {
                        InputStream inputStream = null;
                        try {
                        URL url = new URL(picURL);                    //服务器地址
                        if (url != null) {
                            //打开连接
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            httpURLConnection.setConnectTimeout(3000);//设置网络连接超时的时间为3秒
                            httpURLConnection.setRequestMethod("GET");        //设置请求方法为GET
                            httpURLConnection.setDoInput(true);                //打开输入流
                            int responseCode = httpURLConnection.getResponseCode();    // 获取服务器响应值
                            if (responseCode == HttpURLConnection.HTTP_OK) {        //正常连接
                                inputStream = httpURLConnection.getInputStream();        //获取输入流
                            }

                            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                            byte[] buffer = new byte[1024];
                            int len = 0;
                            while((len = inputStream.read(buffer)) != -1)
                            {
                                outStream.write(buffer,0,len);
                            }

                            byte[] bt = outStream.toByteArray();

                            Log.d("ffff","123456789");
                            bitmap = BitmapFactory.decodeByteArray(bt, 0, bt.length);

                            Message msg = new Message();
                            msg.what = 1;
                            handler.sendMessage(msg);
                        }
                        } catch (Exception e) {
                            e.printStackTrace();
                            setTitle(e.getMessage());
                        }
                    }
                }.start();

            }
        });
    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    // 关闭
                    Log.d("ffff","123456789");
                     image.setImageBitmap(bitmap);

                    break;
            }
        }
    };

}
